package com.mindskip.xzs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindskip.xzs.domain.Batch;
import com.mindskip.xzs.domain.BatchTask;
import com.mindskip.xzs.domain.TaskExam;
import com.mindskip.xzs.repository.BatchMapper;
import com.mindskip.xzs.service.BatchServer;
import com.mindskip.xzs.service.BatchTaskServer;
import com.mindskip.xzs.service.TaskServer;
import com.mindskip.xzs.viewmodel.batch.BatchPageRequestVm;
import com.mindskip.xzs.viewmodel.batch.BatchPageResponseVM;
import com.mindskip.xzs.viewmodel.batch.BatchTaskEditResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

// 标注为Service层的实现类
@Service
public class BatchServerImpl extends ServiceImpl<BatchMapper, Batch> implements BatchServer {

    // 注入BatchTaskServer和TaskServer依赖
    @Resource
    private BatchTaskServer batchTaskServer;
    @Resource
    private TaskServer taskServer;

    // 获取批次分页信息
    @Override
    public BatchPageResponseVM getBatchPage(BatchPageRequestVm batchPageRequestVm) {
        // 创建查询条件包装器
        QueryWrapper<Batch> batchQueryWrapper = new QueryWrapper<>();
        // 创建对象映射器，用于将对象转换为其他类型
        ObjectMapper objectMapper = new ObjectMapper();
        // 创建分页信息，设置当前页码和每页大小
        Page<Batch> pageInfo = new Page<>(batchPageRequestVm.getPageIndex(), batchPageRequestVm.getPageSize());

        // 如果请求参数中的名称不为空，则添加模糊查询条件
        if(!Objects.equals(batchPageRequestVm.getName(), "")) {
            batchQueryWrapper.like("name", batchPageRequestVm.getName());
        }

        // 执行分页查询
        IPage<Batch> iPage = this.page(pageInfo, batchQueryWrapper);
        BatchPageResponseVM batchPageResponseVM = new BatchPageResponseVM();
        List<BatchTaskEditResponse> batchTaskEditResponses = new LinkedList<>();

        // 遍历查询出的批次记录
        for(Object batch : iPage.getRecords()) {
            BatchTaskEditResponse batchTaskEditResponse = new BatchTaskEditResponse();
            // 将查询结果转换为Batch对象并获取其ID和名称
            batchTaskEditResponse.setId(objectMapper.convertValue(batch, Batch.class).getId());
            batchTaskEditResponse.setName(objectMapper.convertValue(batch, Batch.class).getName());
            // 根据批次ID获取批次任务
            BatchTask batchTask = batchTaskServer.getBatchTaskByBatchId(objectMapper.convertValue(batch, Batch.class).getId());
            // 根据任务ID获取任务考试信息
            TaskExam taskExam = taskServer.getById(batchTask.getTaskId());
            // 设置任务名称，如果任务考试信息不存在则设置为空字符串
            if(taskExam == null) {
                batchTaskEditResponse.setTaskName("");
            } else {
                batchTaskEditResponse.setTaskName(taskExam.getTitle());
            }
            // 将批次任务响应对象添加到列表中
            batchTaskEditResponses.add(batchTaskEditResponse);
        }

        // 设置响应对象的列表、总记录数和总页数
        batchPageResponseVM.setList(batchTaskEditResponses);
        batchPageResponseVM.setTotal(iPage.getTotal());
        batchPageResponseVM.setPageNum(iPage.getPages());

        // 返回分页响应对象
        return batchPageResponseVM;
    }

}
